<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Mainmenu_model extends Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

   function list_mainmenu($limit, $start, $customlinks_page_id)
    {  // echo "limit".$limit."start".$start;
        try{
		$where="";
            if (!empty($customlinks_page_id)){
                $where=" where c.customlinks_page_id='{$customlinks_page_id}' ";
            }
            $mainmenu_sql = "select a.mainmenu_id,                    
                    (select link_text from tbl_links b inner join tbl_customlinks d on b.id=d.customlinks_page_id where d.customlinks_id=a.customlinks_id) as customlinks_id ,
                    (select lvalue from tbl_lang b where b.lkey=c.title and b.lang='UK' and module='customlinks') as customlink_title ,
                    (select lvalue from tbl_lang b where b.lkey=a.title and b.lang='UK' and module='mainmenu') as title ,
                    (select lvalue from tbl_lang b where b.lkey=a.shortdesc and b.lang='UK' and module='mainmenu') as `shortdesc` ,
                    (select lvalue from tbl_lang b where b.lkey=a.desc and b.lang='UK' and module='mainmenu') as `desc` ,
                    a.`status`
                    from tbl_mainmenu a 
                    left join tbl_customlinks c on a.customlinks_id=c.customlinks_id 
                    {$where}
                    order by customlinks_id, title, mainmenu_id
                          ";
           $mainmenu_sql.= " limit ".$start.",".$limit;
           //echo $mainmenu_sql;
            $mainmenu = $this->db->query($mainmenu_sql);
            if($mainmenu->num_rows() > 0) {
                 return $mainmenu_result = $mainmenu->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
  function list_mainmenu_count()
    {
        try{
            $mainmenu_sql = "select  count(*) as count
                    from tbl_mainmenu  
                          ";

            $mainmenu = $this->db->query($mainmenu_sql);
            if($mainmenu->num_rows() > 0) {
                  $mainmenu_result = $mainmenu->row_array();
                  return $mainmenu_result["count"];
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    function savemainmenu($post_arr, $langarr)
    {
        $this->load->database();
    	$mainmenudetailsarray=array();
        $last_insert_id=-1;
        $mainmenudetailsarray["customlinks_id"]=$post_arr['customlinks_id'];        
        $mainmenudetailsarray["status"]=$post_arr['status'];
        
        $sql = "INSERT INTO tbl_mainmenu(customlinks_id, status)
                VALUES('" . $mainmenudetailsarray["customlinks_id"] . "', '" . $mainmenudetailsarray["status"] . "')";
       $query = $this->db->query($sql);
    	if($query)
    	{
            $last_insert_id = $this->db->insert_id();
            $mainmenu_title_lang_key = $last_insert_id . "_title";
            $mainmenu_desc_lang_key = $last_insert_id . "_desc";
            $mainmenu_shortdesc_lang_key = $last_insert_id . "_shortdesc";

            $sql = "UPDATE tbl_mainmenu SET title = '" . $mainmenu_title_lang_key . "', shortdesc='" . mysql_real_escape_string($mainmenu_shortdesc_lang_key) . "', `desc` = '" . mysql_real_escape_string($mainmenu_desc_lang_key) . "'
                    WHERE mainmenu_id = " . $last_insert_id;
            $query = $this->db->query($sql);
            if($query)
            {
                foreach ($langarr as $langkey=>$langval)
                {
                    $mainmenudetailsarray['title'.$langkey]=$post_arr['title'.$langkey];
                    $mainmenudetailsarray['shortdesc'.$langkey]=$post_arr['shortdesc'.$langkey];
                    $mainmenudetailsarray['desc'.$langkey]=$post_arr['desc'.$langkey];
                    $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                            VALUES('" . $mainmenu_title_lang_key . "', '" . $langkey . "', '" . $mainmenudetailsarray["title" . $langkey] . "', " . $last_insert_id . ", 'mainmenu')";
                    $query = $this->db->query($sql);
                    if($query)
                    {
                        $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                            VALUES('" . mysql_real_escape_string($mainmenu_desc_lang_key) . "', '" . $langkey . "', '" . $mainmenudetailsarray["desc" . $langkey] . "', " . $last_insert_id . ", 'mainmenu')";
                        $query = $this->db->query($sql);
                        $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                            VALUES('" . mysql_real_escape_string($mainmenu_shortdesc_lang_key) . "', '" . $langkey . "', '" . $mainmenudetailsarray["shortdesc" . $langkey] . "', " . $last_insert_id . ", 'mainmenu')";
                        $query = $this->db->query($sql);
                        
                    }
                }
            }
        }
        return $last_insert_id;
           
    }
    
    function get_mainmenu_details_by_id($mainmenu_id)
    {
        try {
            $mainmenu_sql = "SELECT *
                FROM tbl_mainmenu 
                WHERE mainmenu_id = ?";
            $mainmenu = $this->db->query($mainmenu_sql, array($mainmenu_id));
            if($mainmenu->num_rows() > 0) {
                 return $mainmenu_result = $mainmenu->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    } 
    
    function get_mainmenu_details_lang_by_id($lkey, $langkey, $module='mainmenu')
    {
        $sql = "SELECT * FROM tbl_lang WHERE lkey = ? and lang=? and module=?";
        $facilty = $this->db->query($sql, array($lkey, $langkey, $module));
        if($facilty->num_rows() > 0) {
             return $fac_result = $facilty->result_array();
        } else {
            return FALSE;
        }
    }

    function get_hotel_list($hotelid)
    {
        if ($hotelid){
            $where=" and a.id={$hotelid} ";
        }
        $sql = "select *,a.id as hotel_id,
                    (select lvalue from tbl_lang b where b.lkey=a.hotelname and b.lang='UK') as hotelname                     
                    from tbl_hotelmaster a where status=1 {$where} order by hotelname";
        $hotels = $this->db->query($sql);
        if($hotels->num_rows() > 0) {
             return $$hotel_result = $hotels->result_array();
        } else {
            return FALSE;
        }
    }

    function updatemainmenu($post_arr, $data)
    {
        //print_r($post_arr['facility']);
        $sql = "UPDATE tbl_mainmenu
                SET customlinks_id='".$post_arr['customlinks_id']."', status = '" . $post_arr['status'] . "'
                WHERE mainmenu_id = " . $post_arr['mainmenu_id'];
        
        $query = $this->db->query($sql);
        if($query)
        {
            foreach($data['partnerlang'] as $langkey=>$langval)
            {
                $sql_name = "UPDATE tbl_lang SET lvalue = '" . $post_arr['title' . $langkey] . "'
                        WHERE lang='" . $langkey . "'
                            AND mod_id = " . $post_arr['mainmenu_id'] . "
                                AND lkey = '" . $post_arr['mainmenu_id'].'_title' . "'
                                    AND module = 'mainmenu' ";
                
                $query_title = $this->db->query($sql_name);

                $sql_desc = "UPDATE tbl_lang SET lvalue = '" . mysql_real_escape_string($post_arr['desc' . $langkey]) . "'
                        WHERE lang='" . $langkey . "'
                            AND mod_id = " . $post_arr['mainmenu_id'] . "
                                AND lkey='".$post_arr['mainmenu_id'].'_desc'."'
                                    AND module = 'mainmenu';";
                
                $query_desc = $this->db->query($sql_desc);
                
                $sql_shortdesc = "UPDATE tbl_lang SET lvalue = '" . mysql_real_escape_string($post_arr['shortdesc' . $langkey]) . "'
                        WHERE lang='" . $langkey . "'
                            AND mod_id = " . $post_arr['mainmenu_id'] . "
                                AND lkey='".$post_arr['mainmenu_id'].'_shortdesc'."'
                                    AND module = 'mainmenu';";
                
                $query_desc = $this->db->query($sql_shortdesc);
            }

            
        }
    }

    function delete_mainmenu($mainmenu_id)
    {
        $del_mainmenu_sql = "DELETE FROM tbl_mainmenu WHERE mainmenu_id = " . $mainmenu_id;
        $mainmenu_qry = $this->db->query($del_mainmenu_sql);
        if($mainmenu_qry)
        {            
            $lang_sql = "DELETE FROM tbl_lang WHERE mod_id = " . $mainmenu_id . " AND module='mainmenu' ";
            $lang_qry = $this->db->query($lang_sql);            
        }
    }

    public function get_menulinks($link_type=''){ 
        $where='';
        if(!empty($link_type)){
            $where=" and link_type='{$link_type}' ";
        }
        $menulinks_sql = "select id customlinks_id, link_text customlinks_page_name from tbl_links a  
                    where a.status=1  {$where}
                ";                   
        $menulinks = $this->db->query($menulinks_sql);
        
        if($menulinks->num_rows() > 0) {
            return $menulinks_result = $menulinks->result_array();
        }else{
            return FALSE;
        }
        
    }
        
    public function get_headermenulinks($customlinks_page_id=''){ 
        $where='';

        if(!empty($customlinks_page_id)){
            $where=" and customlinks_page_id='{$customlinks_page_id}' ";
        }
        $menulinks_sql = "select customlinks_id, 
                            (select link_text from tbl_links b  where b.id=a.customlinks_page_id) customlinks_page_name,
                            (select lvalue from tbl_lang b where b.lkey=a.title and b.lang='UK' and module='customlinks') as title
                            from tbl_customlinks a
                    where a.status=1  {$where}
                    order by customlinks_page_name, title
                ";                   
        $menulinks = $this->db->query($menulinks_sql);
        
        if($menulinks->num_rows() > 0) {
            return $menulinks_result = $menulinks->result_array();
        }else{
            return FALSE;
        }
        
    }
    
    public function get_custom_list_id($customlinks_id=''){ 
        $where='';
        if(!empty($customlinks_id)){
            $where=" where customlinks_id='{$customlinks_id}' ";
        }else{
            return array(0=>array('mainmenu_id'=>0));
        }
        $mainmenu_sql = "select mainmenu_id from tbl_mainmenu a  
                    {$where}
                ";                   
        $mainmenu_id = $this->db->query($mainmenu_sql);
        
        if($mainmenu_id->num_rows() > 0) {
            return $mainmenu_id_result = $mainmenu_id->result_array();
        }else{
            return array(0=>array('mainmenu_id'=>0));
        }
        
    }
    
}
?>
